package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.server.mapper.DriverHelppayMapper;
import com.ybkj.daijia.server.mapper.DriverHelppayRecordMapper;
import com.ybkj.daijia.server.model.DriverHelppay;
import com.ybkj.daijia.server.model.HelpPayRecord;
import com.ybkj.daijia.server.service.DriverHelppayService;
import java.math.BigDecimal;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhyu
 * @date 2019/5/8 18:06 desc:
 */
@Service
public class DriverHelppayServiceImpl implements DriverHelppayService {

    public static final Logger log = LoggerFactory.getLogger(DriverHelppayService.class);
    @Autowired
    private DriverHelppayMapper mapper;
    @Autowired
    private DriverHelppayRecordMapper recordMapper;

    /***
     * 后端操作调用
     * @param record
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void operateDriverMoney(HelpPayRecord record) {
        log.debug("operateDriverMoney");
        if (record.getOperateType() == HelpPayRecord.DRIVER_HELPPAY) {
            log.error("支付流程不能调, return");
            return;
        }
        long driverId = record.getDriverId();
        DriverHelppay driverHelppay = mapper.selectOne(driverId);
        boolean save = false;
        if (null == driverHelppay) {
            driverHelppay = new DriverHelppay();
            driverHelppay.setDriver_id(driverId);
            driverHelppay.setHelppayMoney(new BigDecimal(0));
            save = true;
        }
        BigDecimal helppayMoney = driverHelppay.getHelppayMoney();
        //当扣除余额不足时
        if (record.getChangeMoney().compareTo(BigDecimal.ZERO) == -1
            && helppayMoney.compareTo(record.getChangeMoney().abs()) == -1) {
            record.setRealChangeMoney(helppayMoney);
            driverHelppay.setHelppayMoney(BigDecimal.ZERO);
            record.setMemo(record.getMemo() + ";余额不足,已设置为0");
        } else {
            driverHelppay.setHelppayMoney(helppayMoney.add(record.getChangeMoney()));
            BigDecimal changeMoney = record.getChangeMoney();
            record.setRealChangeMoney(changeMoney);
            if (changeMoney.compareTo(BigDecimal.ZERO) == -1) {
                record.setMemo(
                    record.getMemo() + ";实际扣除" + record.getChangeMoney().doubleValue() + "元;当前余额"
                        + driverHelppay.getHelppayMoney().doubleValue());
            } else {
                record.setMemo(
                    record.getMemo() + ";实际充值" + record.getChangeMoney().doubleValue() + "元;当前余额"
                        + driverHelppay.getHelppayMoney().doubleValue());
            }
        }
        recordMapper.insert(record);
        if (save) {
            mapper.insert(driverHelppay);
        } else {
            mapper.updateMoney(driverId, driverHelppay.getHelppayMoney());
        }
    }

    /***
     * 支付操作调用
     * @param record
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void userDriverMoney(HelpPayRecord record) {
        //支付流程才能调
        if (record.getOperateType() == HelpPayRecord.DRIVER_HELPPAY) {
            DriverHelppay driverHelppay = mapper.selectOne(record.getDriverId());
            if (null == driverHelppay) {
                driverHelppay = new DriverHelppay();
                driverHelppay.setDriver_id(record.getDriverId());
                driverHelppay.setHelppayMoney(BigDecimal.ZERO);
            }
            BigDecimal changeMoney = record.getChangeMoney();
            BigDecimal resultMoney = driverHelppay.getHelppayMoney();

            resultMoney = resultMoney.subtract(changeMoney);
            record.setRealChangeMoney(changeMoney);
            record.setMemo(
                record.getMemo() + ";实际扣除" + changeMoney.doubleValue() + "元;当前余额" + driverHelppay
                    .getHelppayMoney().doubleValue());
            recordMapper.insert(record);
            mapper.updateMoney(record.getDriverId(), resultMoney);
        }
    }

    public List<DriverHelppay> getList() {
        return null;
    }

    @Override
    public boolean checkHelpMoneyEnough(long driverId, BigDecimal costMoney) {
        DriverHelppay driverHelppay = mapper.selectOne(driverId);
        if (costMoney.compareTo(BigDecimal.ZERO) == -1) {
            return false;
        }
        if (null != driverHelppay) {
            if (driverHelppay.getHelppayMoney().compareTo(costMoney) == -1) {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    @Override
    public BigDecimal getHelppayMoneyById(long driverId) {
        return mapper.getHelppayMoneyById(driverId);
    }
}
